package com.egame.manage.biz.server.gc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cn.egame.common.data.BaseDao;
import cn.egame.common.exception.ExceptionCommonBase;
import com.egame.manage.biz.domain.AuditInfo;

public class AuditServiceDao extends BaseDao {

	public AuditServiceDao() throws ExceptionCommonBase {
		super("egame_ext");		
	}

	public int setAuditLog(int gId,int gameStatus,long operator)throws ExceptionCommonBase{
		String sql = "insert into t_game_audit(g_id,game_status,operator,insert_time) values(" +
				gId + "," +
				gameStatus + "," +
				operator + "," +
				new Date().getTime() + ")";
		return this.executeUpdate(sql);
	}
	
	private AuditInfo formatAuditInfo(ResultSet rs) throws ExceptionCommonBase {
		AuditInfo item = new AuditInfo();
		try {
			int i = 1;
			item.setAuditId(rs.getLong(i++));
			item.setgId(rs.getInt(i++));
			item.setGameStatus(rs.getInt(i++));
			item.setOperatorId(rs.getLong(i++));
			item.setInsertTime(rs.getLong(i++));
		} catch (SQLException e) {
			throw ExceptionCommonBase.throwExceptionCommonBase(e);
		}		
		return item;
	}
	

	public List<AuditInfo> listAuditInfoByGid(int gid)throws ExceptionCommonBase {
		String sql = "SELECT audit_id,g_id,game_status,operator,insert_time FROM t_game_audit " 
				+ " where g_id = " + gid;
		Connection conn = this.getConnection();
		ResultSet rs = this.executeQuery(conn, sql);
		try {
			List<AuditInfo> list = new ArrayList<AuditInfo>();
			while (rs.next()) {
				list.add(formatAuditInfo(rs));
			}
			return list;
		} catch (SQLException e) {
			throw ExceptionCommonBase.throwExceptionCommonBase(e);
		} finally {
			this.close(rs);
			this.releaseConnection(conn);
		}
	}
}
